﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace BakalarskaPraca.Generation.Helpers
{
    public class XmlHelper
    {
        public static bool MergeXmlNodes(XmlDocument sourceDocument, XmlDocument targetDocument, string sourceTagName, string targetTagName = "")
        {
            if (targetTagName == "")
            {
                targetTagName = sourceTagName;
            }
            XmlNodeList sourceNodeList = sourceDocument.GetElementsByTagName(sourceTagName);
            if (sourceNodeList.Count == 0)
            {
                return false;
            }
            XmlNodeList targetNodeList = targetDocument.GetElementsByTagName(targetTagName);
            XmlNode targetNode;
            if (targetNodeList.Count > 0)
            {
                targetNode = targetNodeList[0];
            }
            else
            {
                targetNode = targetDocument.CreateNode(XmlNodeType.Element, targetTagName, "");
                targetDocument.DocumentElement.AppendChild(targetNode);
            }
            XmlNode sourceNode = sourceNodeList[0];
            if (sourceNode.ChildNodes.Count == 0)
            {
                return false;
            }
            foreach (XmlNode node in sourceNode.ChildNodes)
            {
                targetNode.AppendChild(targetDocument.ImportNode(node, true));
            }
            Console.WriteLine(sourceTagName + " : sucess");
            return true;
        }
    }
}
